struct Solution {}

fn main() {}

// Definition for a binary tree node.
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
  pub val: i32,
  pub left: Option<Rc<RefCell<TreeNode>>>,
  pub right: Option<Rc<RefCell<TreeNode>>>,
}

impl TreeNode {
  #[inline]
  pub fn new(val: i32) -> Self {
    TreeNode {
      val,
      left: None,
      right: None
    }
  }
}

use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
    pub fn is_same_tree(p: Option<Rc<RefCell<TreeNode>>>, q: Option<Rc<RefCell<TreeNode>>>) -> bool {
        Self::do_preorder(p, q)
    }

    fn do_preorder(p: Option<Rc<RefCell<TreeNode>>>, q: Option<Rc<RefCell<TreeNode>>>) -> bool {
        if p.is_none() && q.is_none() {
            return true;
        }
        if p.is_some() && q.is_none() {
            return false;
        }
        if p.is_none() && q.is_some() {
            return false;
        }
        if p.clone().unwrap().borrow().val != q.clone().unwrap().borrow().val {
            return false;
        }
        let left = Self::do_preorder(p.clone().unwrap().borrow().left.clone(), q.clone().unwrap().borrow().left.clone());
        if !left {
            return false;
        }
        return Self::do_preorder(p.clone().unwrap().borrow().right.clone(), q.clone().unwrap().borrow().right.clone());
    }
}